<?php
namespace Emoney\EpayBundle\Api;
/**
 * Description of CrystalMoney
 *
 * @author COMPAQ
 */
class CrystalMoney {
    //put your code here
    
    /**
     * security key of the customer
     * @var type 
     */
    protected $_key = '';
    /**
     * version of the api
     * @var type 
     */
    protected  $_version = '';
    /**
     * Customer personnal code
     * @var type 
     */
    protected $_customerKey = '';
    /**
     * Amount of the customer command
     * @var type 
     */
    protected $_amount = '';
    /**
     * Url of the thread server
     * @var type 
     */
    //protected $_serverUrl = '';
    /**
     * return url after transaction ok
     * @var type 
     */
    protected $_returnOkUrl = '';
    /**
     * return url after transaction error
     * @var type 
     */
    protected $_returnErrorUrl = '';
    /**
     *email of the site customer
     * @var type 
     */
    protected $_email = '';
    /**
     * date of operation
     * @var type 
     */
    protected $_dateOp ='';
    /**
     * currency of site customer
     * @var type 
     */
    protected $_currency = '';
    /**
     * key generate by this api and used by Crystal server to compare different data
     * @var type 
     */
    protected $_cryptKey = '';
    
    /**
     * site name of customer
     * @var type 
     */
    protected $_sitename = '';

    /**
     * order identification
     * @var type 
     */
    protected $_transactionId = '';

    /**
     * customer site Url
     * @var type 
     */
    protected $_siteUrl = '';
    /**
     * instance of api
     * @param array $param 
     */
    public function __construct(array $param = array()) {
        //var_dump($param);
        if (!empty($param)){
            $this->hydrate($param);
        }
        $vars_class = get_class_vars(get_class($this));
        $dataToCrypt ='';
        
        foreach ($vars_class as $key => $value) {
            $getMethode = 'get'.ucfirst(substr($key, 1));
            if(method_exists($this, $getMethode) && $getMethode !='getCryptKey')
                $dataToCrypt .= $this->$getMethode().'*';
        }
        
        $this->computeHmac($dataToCrypt);
        
    }
    /**
     *initialise différent property
     * @param array $donnees 
     */
    private function hydrate(array $donnees){
        foreach ($donnees as $attribut => $valeur){
            if(property_exists($this, '_'.$attribut)){
                $methode = 'set'.ucfirst($attribut);
                if (is_callable(array($this, $methode))){
                    $this->$methode($valeur);
                }
            }
        }
    }
    
    /**
     * Fonction / Function : computeHmac
     * Renvoie le sceau HMAC d'une chaine de données
     * @param type $sData
     * @return type 
     */
    private function computeHmac($sData) {

		$this->setCryptKey(strtolower(hash_hmac("sha1", $sData, $this->getKey())));
	}
    /**
     *
     * @param type $key 
     */
    public function setKey($key){
        $this->_key = $key;
    }
    /**
     *
     * @param type $version 
     */
    public function setVersion($version){
        $this->_version = $version;
    }
    /**
     *
     * @param type $customerKey 
     */
    public function setCustomerKey($customerKey){
        $this->_customerKey = $customerKey;
    }
	/**
     *
     * @param type $amount 
     */
	public function setAmount($amount){
        $this->_amount = $amount;
    }
    /**
     *
     * @param type $serverUrl 
     */
    public function setServerUrl($serverUrl){
        $this->_serverUrl = $serverUrl;
    }
    /**
     *
     * @param type $returnOkUrl 
     */
    public function setReturnOkUrl($returnOkUrl){
        $this->_returnOkUrl = $returnOkUrl;
    }
    /**
     *
     * @param type $returnErrorUrl 
     */
    public function setReturnErrorUrl($returnErrorUrl){
        $this->_returnErrorUrl = $returnErrorUrl;
    }
	/**
     *
     * @param type $email 
     */
	public function setEmail($email){
        $this->_email = $email;
    }
	/**
     *
     * @param type $dateOp 
     */
    public function setDateOp($dateOp){
        $this->_dateOp = $dateOp;
    }
    /**
     *
     * @param type $currency 
     */
    public function setCurrency($currency){
        $this->_currency = $currency;
    }
    /**
     *
     * @param type $cryptKey 
     */
    public function setCryptKey($cryptKey){
        $this->_cryptKey = $cryptKey;
    }

    /**
     *
     * @param type $sitename 
     */
    public function setSitename($sitename){
        $this->_sitename = $sitename;
    }

    /**
     *
     * @param type $transactionId 
     */
    public function setTransactionId($transactionId){
        $this->_transactionId = $transactionId;
    }

    /**
     * init customer site url
     * @param type $siteUrl 
     */
    public function setSiteUrl($siteUrl){
        $this->_siteUrl = $siteUrl;
    }
    //GETTERS
    /**
     *
     * @return type 
     */
    public function getKey(){
        return $this->_key;
    }
    /**
     *
     * @return type 
     */
    public function getVersion(){
        return $this->_version;
    }
    /**
     *
     * @return type 
     */
    public function getCustomerKey(){
        return $this->_customerKey;
    }
	/**
     *
     * @return type 
     */
    public function getAmount(){
        return $this->_amount;
    }
	/**
     *
     * @return type 
     */
    public function getServerUrl(){
        return $this->_serverUrl;
    }
	/**
     *
     * @return type 
     */
    public function getReturnOkUrl(){
        return $this->_returnOkUrl;
    }
	/**
     *
     * @return type 
     */
    public function getReturnErrorUrl(){
        return $this->_returnErrorUrl;
    }
	/**
     *
     * @return type 
     */
    public function getEmail(){
        return $this->_email;
    }
	/**
     *
     * @return type 
     */
    public function getDateOp(){
        return $this->_dateOp;
    }
	/**
     *
     * @return type 
     */
    public function getCurrency(){
        return $this->_currency;
    }
	/**
     *
     * @return type 
     */
    public function getCryptKey(){
        return $this->_cryptKey;
    }
	/**
     *
     * @return type 
     */
    public function getSitename(){
        return $this->_sitename;
    }
    /**
     *
     * @return type 
     */
    public function getTransactionId(){
        return $this->_transactionId;
    }
	
    /**
     * get customer site url
     * @return type 
     */
    public function getSiteUrl(){
        return $this->_siteUrl;
    }
}

?>
